<?php

namespace app\admin\controller\lists;

use app\common\controller\Backend;
use think\Config;
use think\Db;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Loader;
use think\Session;

/**
 * 服务单录入查询需要的字段集合
 *
 * @icon fa fa-circle-o
 */
class Pigai extends Backend
{

    /**
     * Pigai模型对象
     * @var \app\admin\model\lists\Pigai
     */
    protected $model = null;
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\lists\Pigai;
        $this->view->assign("statusList", $this->model->getStatusList());
        $jigoudata = [''=>'请选择业务来源','直属业务'=>'直属业务','直接业务'=>'直接业务','分支机构'=>'分支机构','合作商'=>'合作商','查勘机构'=>'查勘机构','招商业务'=>'招商业务','合作业务 '=>'合作业务'];
        $this->view->assign('jigoudata', $jigoudata);
        $site = Config::get('site');
        $this->view->assign('chexing', $site['chexing']);
        $this->view->assign('xingzhi', $site['xingzhi']);
        $admin = $this->auth->getUserInfo();
        $rule = Db::name('auth_group_access')->alias('a')->join('auth_group b','a.group_id = b.id')->where('a.uid',$admin['id'])->field('b.rules,a.group_id')->find();
        $rules = $rule['rules'];
        if($rules == '*'){
            $rules = Db::name('auth_rule')->column('id');
        }else{
            $rules = explode(',',$rule['rules']);
        }
        $this->rule = $rules;
        $this->group_id = $rule['group_id'];
        $this->assignconfig('rule', $rules);
        $this->assignconfig('admin', $this->auth->getUserInfo());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            $filter = json_decode($this->request->get("filter", ''), true);
            $op = json_decode($this->request->get("op", '','trim'), true);
            $where0 = [];
            $admin = $this->auth->getUserInfo();
            //当前登录人组织
            $cateid = $admin['zuzhi'];
            if (isset($filter['zuzhi.name'])) {
                if($filter['zuzhi.name']) $zuzhi = $filter['zuzhi.name'];
                // 处理完可以去掉也可以覆盖重构
                unset($filter['zuzhi.name'],$op['zuzhi.name']);
            }
            if($admin['id'] == 1 && !isset($zuzhi)){
                $arr = [];
            }else{
                //查询所有租住
                $date = Db::name('zuzhi')->where('delete_id',0)->select();
                //有组织筛选就赋值给cateid
                if(isset($zuzhi) && $zuzhi){
                    $cateid = $zuzhi;
                }
                //出理上下级关系
                $arr = $this->frontdels($date,$cateid);
                //赋值给z_arr
                $arr[]=$cateid;
            }
            if($arr){
                $where0['pigai.yw_class'] = ['in',$arr];
            }
            $this->request->get(['filter'=>json_encode($filter,true)]);
            $this->request->get(['op'=>json_encode($op,true)]);
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
                ->with(["zuzhi"])
                ->where($where)
                ->where($where0)
                ->order($sort, $order)
                ->paginate($limit);
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 查询批单提交信息是否存在
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function chaxun(){
        $data = input('get.');
        $map = array();
        $car_engine = $data['car_engine'];
        if($car_engine){
            $map['car_engine']=$car_engine;
        }
        $tong_number = $data['tong_number'];
        if($tong_number){
            $map['tong_number']=$tong_number;
        }
        $car_vin = $data['car_vin'];
        if($car_vin){
            $map['car_vin']=$car_vin;
        }
        $car_num = $data['car_num'];
        if($car_num){
            $map['car_num']=$car_num;
        }
        $map['status']=5;
        $row = db('list_one')->where($map)->where('deletetime',)->find();
        if (!$row) {
            $row1 = db('list_one_old')->where($map)->field('id,createtime,updatetime,deletetime,ids,gs_type',1)->find();
            if($row1){
                if($row1['gs_type'] <> 5){
                    $this->error('无权限操作');
                }
                $row1['dianwang'] = $row1['ziran'];
                $row1['dw_tong'] = $row1['zi_tong'];
                $row1['dw_biao'] = $row1['zi_biao'];
                $row1['dw_zhe'] = $row1['zi_zhe'];
                $row1['dw_ying'] = $row1['zi_ying'];
                $row1['dw_ji'] = $row1['zi_buji'];
                unset($row1['ziran_c']);
                unset($row1['ziran']);
                unset($row1['zi_tong']);
                unset($row1['zi_biao']);
                unset($row1['zi_zhe']);
                unset($row1['zi_ying']);
                unset($row1['zi_buji']);
                $row1['yw_class'] = 1;
                Db::name('list_one')->insert($row1);
                $this->success();
            }else{
                $this->error('没找到相关信息');
            }
        }else{
            $info = Db::name('list_three')->where('ids',$row['id'])->where('deletetime',)->whereNotIn('status',[0,4,5])->find();
            if($info) $this->error('存在审核中的批单');
            $this->success();
        }
    }

    /**
     * 批单跳转
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function allp()
    {
        $data = input('get.');
        $this->assign('batch_state', $data['batch_state']);
        $map = array();
        $car_engine = $data['car_engine'];
        if($car_engine){
            $map['car_engine']=$car_engine;
        }
        $tong_number = $data['tong_number'];
        if($tong_number){
            $map['tong_number']=$tong_number;
        }
        $car_vin = $data['car_vin'];
        if($car_vin){
            $map['car_vin']=$car_vin;
        }
        $car_num = $data['car_num'];
        if($car_num){
            $map['car_num']=$car_num;
        }
        $map['status']=5;
        $row = db('list_one')->where($map)->where('deletetime',)->find();
        if (!$row) {
            $this->error('没找到相关信息');
        }
        $id = $row['id'];
        $row['car_register'] = date('Y-m-d',$row['car_register']);
        $this->assign('row', $row);
        $admin = $this->auth->getUserInfo();
        $data = db('zuzhi')->where('delete_id',0)->select();
        $arr = $this->frontdels($data,$admin['zuzhi']);
        $arr[] = $admin['zuzhi'];
        $ren = db('admin')->where('zuzhi','in',$arr)->select();
        $select = db('zuzhi')->where('id','in',$arr)->select();
        $this->assign('ren',$ren);
        $this->assign('selectMenu',$select);
        $ying  = db('tong_files')->where('tong_id',$id)->column('url');
        $this->assign('file', implode(',',$ying));
        return $this->fetch();
    }
    /**
     * 编辑
     *
     * @param $ids
     * @return string
     * @throws DbException
     * @throws \think\Exception
     */
    public function edit($ids = null,$status = 0)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $admin = $this->auth->getUserInfo();
        if ($row['status']==5){
            $this->error('已生成服务单不可修改');
        }
        if ($row['status']==1 || $row['status']==2 || $row['status']==3){
            $this->error('审核中,禁止操作');
        }
        $row['car_register'] = date('Y-m-d',$row['car_register']);
        $this->view->assign('row', $row);
        $res = db('zuzhi')->where('jigou', $row['yw_come'])->where('delete_id',0)->select();
        $cateid = $admin['zuzhi'];
        $arr_all = $this->frontdels($res, $cateid);
        $arr_all[] = $cateid;
        $select = db('zuzhi')
            ->where('jigou', $row['yw_come'])
            ->where('id','in',$arr_all)
            ->select();
        $select = Loader::model('Zuzhi')->selectMenus_res($select);
        $this->assign('selectMenu',$select);
        $ren = db('admin')->where('zuzhi','in',$row['yw_class'])->select();
        $this->assign('ren',$ren);
        $ying = db('tong_files')->where('tong_id',$row['ids'])->column('url');
        $this->assign('file', implode(',',$ying));
        $this->assign('batch_state',$row['batch_state']);

        $yijian = db('yijian')->where('did',$row['ids'])->select();
        $this->assign('yijian',$yijian);
        return $this->view->fetch();
    }

    /**
     * 处理组织
     * @param $data
     * @param $cateid
     * @return array
     */
    public function frontdels($data,$cateid){
        static $arr=array();
        foreach ($data as $key => $v) {
            if($v["pid"]==$cateid){
                $arr[]=$v['id'];
                $this->frontdels($data,$v['id']);
            }
        }
        return $arr;
    }

    /**
     * 批改提交
     */
    public function upshens(){
        if (request()->isAjax()) {
            $xiudata = $this->request->post('row/a');
            $id = 0;
            if(isset($xiudata['idi']) && $xiudata['idi']){
                $id = $xiudata['idi'];
                unset($xiudata['idi']);
            }
            unset($xiudata['contents']);
            unset($xiudata['kehu_sumdiscount']);
            unset($xiudata['kehu_tongchou']);
            unset($xiudata['tui_time']);
            $xiudata['pigai_type'] = 1;
            $xiudata['status']=2;
            $xiudata['ids']=$xiudata['id'];
//            $xiudata['yw_join_time'] = strtotime($xiudata['yw_join_time']);
//            $xiudata['yw_hetong_time'] = strtotime($xiudata['yw_hetong_time']);
//            $xiudata['yw_shenhe_time'] = strtotime($xiudata['yw_shenhe_time']);
//            $xiudata['give_starttime'] = strtotime($xiudata['give_starttime']);
//            $xiudata['give_endtime'] = strtotime($xiudata['give_endtime']);
            $xiudata['car_register'] = strtotime($xiudata['car_register']);
//            $xiudata['car_issue_time'] = strtotime($xiudata['car_issue_time']);
            $xiudata['plan_strattime'] = strtotime($xiudata['plan_strattime']);
            $xiudata['plan_endtime'] = strtotime($xiudata['plan_endtime']);
            //1、获取本商户的最新进货单号
            $xiudata['pigai_numz'] = zancun("list_three", "pigai_numz", "P");
            $xiudata['pigai_ctime'] = time();
            if (isset($xiudata['one_id']) && $xiudata['one_id']) {
                $one_id = $xiudata['one_id'];
            } else {
                $one_id = $xiudata['id'];
            }
            unset($xiudata['id']);
            unset($xiudata['tong']);
            $urls = $xiudata['file'];
            unset($xiudata['file']);
            unset($xiudata['chexing_ci']);
            unset($xiudata['feiyong']);
            // 启动事务
            Db::startTrans();
            try {
                $xiudata['car_is_new'] = isset($xiudata['car_is_new']) ? $xiudata['car_is_new'] : 2;
                $xiudata['main_franchise'] = isset($xiudata['main_franchise']) ? $xiudata['main_franchise'] : 2;
                $xiudata['main_ji'] = isset($xiudata['main_ji']) ? $xiudata['main_ji'] : 2;
                $xiudata['dao_ji'] = isset($xiudata['dao_ji']) ? $xiudata['dao_ji'] : 2;
                $xiudata['siji_ji'] = isset($xiudata['siji_ji']) ? $xiudata['siji_ji'] : 2;
                $xiudata['ck_ji'] = isset($xiudata['ck_ji']) ? $xiudata['ck_ji'] : 2;
                $xiudata['dw_ji'] = isset($xiudata['dw_ji']) ? $xiudata['dw_ji'] : 2;
                $xiudata['siji_ji'] = isset($xiudata['siji_ji']) ? $xiudata['siji_ji'] : 2;
                $xiudata['main_name'] = isset($xiudata['main_name']) ? $xiudata['main_name'] : 2;
                $xiudata['main_disan'] = isset($xiudata['main_disan']) ? $xiudata['main_disan'] : 2;
                $xiudata['dao_name'] = isset($xiudata['dao_name']) ? $xiudata['dao_name'] : 2;
                $xiudata['siji'] = isset($xiudata['siji']) ? $xiudata['siji'] : 2;
                $xiudata['chengke'] = isset($xiudata['chengke']) ? $xiudata['chengke'] : 2;
                $xiudata['dianwang'] = isset($xiudata['dianwang']) ? $xiudata['dianwang'] : 2;
                $xiudata['pigai_type']=1;
                $xiudata['status']=2;
                header("Content-Type: text/html; charset=utf-8");
                // 修改日志
                $changes = $this->recordChanges($one_id, $xiudata);
                db('change_log')->insert(['oneid'=>$one_id, 'content'=>$changes]);
                if (!$id){
                    $admin = $this->auth->getUserInfo();
                    $xiudata['lururen'] = $admin['username'];
                    $xiudata['lururen_id'] = $admin['id'];
                    $uplistone = db('list_three')->insertGetId($xiudata);
                } else {
                    if($xiudata['one_id']){
                        unset($xiudata['one_id']);
                    }
                    unset($xiudata['ids']);
                    $uplistone = db('list_three')->where('id', $id)->update($xiudata);
                }
                //保存图片
                $one = new One();
                $res = $one->saveFile($one_id, $urls);
                if ($uplistone) {
                    // 提交事务
                    Db::commit();
                } else {
                    Db::rollback();
                    $this->error('提交失败');
                }
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
                $this->error('报错了'.$e->getMessage());
            }
        }
        $this->success();
    }

    public function scpiwen(){
        $xiudata = $this->request->post('row/a');
        unset($xiudata['contents']);
        unset($xiudata['kehu_sumdiscount']);
        unset($xiudata['kehu_tongchou']);
        unset($xiudata['tui_time']);
        $xiudata['pigai_type'] = 1;
        $xiudata['status']=2;
        $xiudata['ids']=$xiudata['id'];
        $xiudata['car_register'] = strtotime($xiudata['car_register']);
        $xiudata['plan_strattime'] = strtotime($xiudata['plan_strattime']);
        $xiudata['plan_endtime'] = strtotime($xiudata['plan_endtime']);
        $xiudata['pigai_ctime'] = time();
        if (isset($xiudata['one_id']) && $xiudata['one_id']) {
            $one_id = $xiudata['one_id'];
        } else {
            $one_id = $xiudata['id'];
        }
        unset($xiudata['id']);
        unset($xiudata['tong']);
        unset($xiudata['file']);
        unset($xiudata['chexing_ci']);
        unset($xiudata['feiyong']);
        $xiudata['car_is_new'] = isset($xiudata['car_is_new']) ? $xiudata['car_is_new'] : 2;
        $xiudata['main_franchise'] = isset($xiudata['main_franchise']) ? $xiudata['main_franchise'] : 2;
        $xiudata['main_ji'] = isset($xiudata['main_ji']) ? $xiudata['main_ji'] : 2;
        $xiudata['dao_ji'] = isset($xiudata['dao_ji']) ? $xiudata['dao_ji'] : 2;
        $xiudata['siji_ji'] = isset($xiudata['siji_ji']) ? $xiudata['siji_ji'] : 2;
        $xiudata['ck_ji'] = isset($xiudata['ck_ji']) ? $xiudata['ck_ji'] : 2;
        $xiudata['dw_ji'] = isset($xiudata['dw_ji']) ? $xiudata['dw_ji'] : 2;
        $xiudata['siji_ji'] = isset($xiudata['siji_ji']) ? $xiudata['siji_ji'] : 2;
        $xiudata['main_name'] = isset($xiudata['main_name']) ? $xiudata['main_name'] : 2;
        $xiudata['main_disan'] = isset($xiudata['main_disan']) ? $xiudata['main_disan'] : 2;
        $xiudata['dao_name'] = isset($xiudata['dao_name']) ? $xiudata['dao_name'] : 2;
        $xiudata['siji'] = isset($xiudata['siji']) ? $xiudata['siji'] : 2;
        $xiudata['chengke'] = isset($xiudata['chengke']) ? $xiudata['chengke'] : 2;
        $xiudata['dianwang'] = isset($xiudata['dianwang']) ? $xiudata['dianwang'] : 2;
        $xiudata['pigai_type']=1;
        $xiudata['status']=2;
        $changes = $this->recordChanges($one_id, $xiudata);
        $this->success('','',$changes);

    }
    /**
     * 处理批文
     * @param $one_id
     * @param $xiu
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function recordChanges($one_id, $xiu) {
        $xiu['yw_join_time'] = date('Y-m-d H:i:s',$xiu['yw_join_time']);
        $xiu['yw_hetong_time'] = date('Y-m-d H:i:s',$xiu['yw_hetong_time']);
        $xiu['yw_shenhe_time'] = date('Y-m-d H:i:s',$xiu['yw_shenhe_time']);
//        $xiu['give_starttime'] = date('Y-m-d H:i:s',$xiu['give_starttime']);
//        $xiu['give_endtime'] = date('Y-m-d H:i:s',$xiu['give_endtime']);
        $xiu['car_register'] = date('Y-m-d H:i:s',$xiu['car_register']);
//        $xiu['car_issue_time'] = date('Y-m-d H:i:s',$xiu['car_issue_time']);
        $xiu['plan_strattime'] = date('Y-m-d',$xiu['plan_strattime']);
        $xiu['plan_endtime'] = date('Y-m-d',$xiu['plan_endtime']);
        $one = db('list_one')->where('id', $one_id)->find();
        $one['yw_join_time'] = date('Y-m-d H:i:s',$one['yw_join_time']);
        $one['yw_hetong_time'] = date('Y-m-d H:i:s',$one['yw_hetong_time']);
        $one['yw_shenhe_time'] = date('Y-m-d H:i:s',$one['yw_shenhe_time']);
//        $one['give_starttime'] = date('Y-m-d H:i:s',$one['give_starttime']);
//        $one['give_endtime'] = date('Y-m-d H:i:s',$one['give_endtime']);
        $one['car_register'] = date('Y-m-d H:i:s',$one['car_register']);
//        $one['car_issue_time'] = date('Y-m-d H:i:s',$one['car_issue_time']);
        $one['plan_strattime'] = date('Y-m-d',$one['plan_strattime']);
        $one['plan_endtime'] = date('Y-m-d',$one['plan_endtime']);
        $changes = array();
        $changes = ['因客户要求，经被服务人申请，本服务单'];
        foreach ($one as $k=>$v) {
            if(isset($this->dict[$k])){
                $kname = $this->dict[$k];
                if ($kname != null) {
                    $v3 = $xiu[$k];
                    if($v3!=''){
                        if ($v3 != $v) {
                            $str = ($this->dict[$k] ? : $k) . '由 ' . $v . ' 变更为 ' . $v3;
                            array_push($changes, $str);
                        }
                    }
                }
            }

        }
        $changes[] = '本服务单其它内容不变，特此批改';
        return implode("\r\n", $changes);
    }
    var $dict = [
        'yw_come' => '业务来源',
        'yw_class' => '归属部门',
        'yw_belonger' => '归属人',
        'yw_handle' => '经办',
        'tong_storage' => '参统单暂存号',
        'tong_number' => '服务单号',
        'tong_document' => '单证号',
        'car_num' => '车牌号',
        'car_is_new' => '是否为新车',
        'haocolor' => '号牌底色',
        'car_owner' => '车主姓名',
        'car_card_kind' => '证件类型',
        'car_card' => '证件号',
        'car_vin' => '车架号',
        'car_engine' => '发动机号',
        'car_factory_num' => '厂牌型号',
        'car_register' => '注册日期',
        'car_buy' => '新车购置价',
        'car_reall' => '实际价值',
        'car_use_nature' => '使用性质',
        'car_leixing' => '车辆类型',
        'car_carry_peo' => '核定载客',
        'car_carry_quality' => '核定载质量',
        'car_curb_weight' => '整备质量',
        'car_lastyear' => '上年出险情况',
        'plan_strattime' => '服务起期',
        'plan_endtime' => '服务止期',
        'main_name' => '损失服务',
        'main_money' => '损失服务金额',
        'main_standard' => '损失标准服务费',
        'main_discount' => '损失折扣',
        'main_submit' => '损失应交服务费',
        'main_franchise' => '损失不计免赔',
        'main_disan' => '机动车第三者责任服务',
        'main_santong' => '第三者服务金额',
        'main_biao' => '第三者标准服务费',
        'main_zhe' => '第三者折扣',
        'main_yingjiao' => '第三者应交服务费',
        'main_ji' => '第三者不计免赔',
        'dao_name' => '机动车全车盗抢服务',
        'dao_tong' => '盗抢服务金额',
        'dao_biao' => '盗抢标准服务费',
        'dao_zhe' => '盗抢折扣',
        'dao_ying' => '盗抢应交',
        'dao_ji' => '盗抢不计',
        'siji' => '服务司机',
        'siji_tong' => '司机服务',
        'siji_biao' => '司机标准',
        'siji_zhe' => '司机折扣',
        'siji_ying' => '司机应交',
        'siji_ji' => '司机不计',
        'chengke' => '服务乘客',
        'ck_money' => '乘客 元/座',
        'chengke_zuo' => '乘客座',
        'ck_tong' => '乘客服务',
        'ck_biao' => '乘客标准',
        'ck_zhe' => '乘客折扣',
        'ck_ying' => '乘客应交',
        'ck_ji' => '乘客不计',
        'main_sum' => '基准服务费用合计',
        'main_sumdiscount' => '合计折扣',
        'main_free' => '服务费合计',
        'learned_name' => '被参统人姓名',
        'learned_kind' => '被参统人证件类型',
        'learned_num' => '被参统人证件号码',
        'learned_phone' => '被参统人手机号',
        'resolvent' => '解决方式',
        'special' => '特别约定',
        'dianwang' => '外部电网',
        'dw_tong' => '外部电网服务费',
        'dw_biao' => '标准服务费',
        'dw_zhe' => '外部电网折扣',
        'dw_ying' => '外部电网应交',
        'dw_ji' => '外部电网不计',
    ];

    /**
     * 撤回
     */
    public function quxiao($ids = null){
        $id = $ids;
        $ress = Db::name('list_three')->where('id',$id)->find();
        if(empty($id)){
            $this->error('请先选择要操作的对象');
        }elseif($ress['status'] == 5){
            $this->error('已生成服务单,不可进行操作');
        }else{
            $res = Db::name('list_three')->where('id',$id)->update(['status'=>4]);
            if($res){
                $this->success('成功');
            }else{
                $this->error('操作失败');
            }

        }
    }
    // 驳回
    // TODO: 记录日志
    public  function quxiao_admin($ids = null){
        $id = $ids;
        if(empty($id)){
            $this->error('请先选择要操作的对象');
        }else{
            $res = Db::name('list_three')->where('id',$id)->update(['status'=>4,'pigai_nump'=>null]);
            if($res){
                $this->success('成功');
            }else{
                $this->error('操作失败');
            }
        }
    }

    /**
     * 浏览
     * @param $ids
     * @return void
     * @throws DbException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function details($ids = null){
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $admin = $this->auth->getUserInfo();
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
            $this->error(__('You have no permission'));
        }
        $row['car_register'] = date('Y-m-d',$row['car_register']);
        $this->view->assign('row', $row);
        $select = selectMenus_zuzhi($admin['zuzhi']);
        $this->assign('selectMenu',$select);
        $ren = db('admin')->where('zuzhi','in',$row['yw_class'])->select();
        $this->assign('ren',$ren);
        $ying = db('tong_files')->where('tong_id',$row['ids'])->select();
        $this->assign('file', $ying);
        $yijian = db('yijian')->where('did',$row['ids'])->select();
        $this->assign('yijian',$yijian);
        return $this->view->fetch();
    }

    /**
     * 打印批文时候获取批文内容
     */
    public function dianpw($ids = null){
        $piwen=db('list_three')->field('pigai_piwen,learned_name,pigai_nump,tong_number,pigai_ctime,yw_hetongpeo,yw_handle,gs_type')->where('id',$ids)->find();
        $this->assign('piwen',$piwen);
        $this->assign('admin',$this->auth->getUserInfo());
        // 临时关闭当前模板的布局功能
        $this->view->engine->layout(false);
        return $this->fetch();
    }

    //补录页面
    public function bulu($ids = null){
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $admin = $this->auth->getUserInfo();
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
            $this->error(__('You have no permission'));
        }
        if (false === $this->request->isPost()) {
            $this->assign('row',$row);
            return $this->view->fetch();
        }
        $params = $this->request->post('row/a');
        if (empty($params)) {
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $params = $this->preExcludeFields($params);
        $result = true;
        Db::startTrans();
        try {
            //是否采用模型验证
            $data['refund_time'] = strtotime($params['refund_time']);
            $data['refund_money'] = $params['refund_money'];
            $data['refund_beizhu'] = $params['refund_beizhu'];
            $data['refund_img'] = $params['file'];
            Db::name('list_three')->where('id',$ids)->update($data);
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if (false === $result) {
            $this->error(__('No rows were updated'));
        }
        $this->success();
    }

}
